<?php

declare(strict_types=1);

namespace app\api\controller;

use Alipay\EasySDK\Kernel\Factory;
use app\common\tool\pay\PayPreset;
use app\common\model\Logs;
use app\common\model\Order;
use app\common\model\User;
use app\common\model\RemitLine;
use app\common\model\Seckill;
use app\common\model\Cart;
use app\common\facade\Pay;
use app\common\facade\Target;
use app\common\facade\OssBackup;

class Callback
{



    //自动备份
    public function ossBackup()
    {
        try {
            $route = 'callback/autoOssBackup.log';
            $result = OssBackup::auto();
            Logs::write($route, json_encode($result));
        } catch (\Exception $e) {
            Logs::write($route, $e->getMessage());
        }
    }
    /**
     *积分清空
     */
    public function clearIntegral()
    {
        try {
            $route = 'callback/clearIntegral.log';
            $result = User::clear_lntegral();
            Logs::write($route, json_encode($result));
        } catch (\Exception $e) {
            Logs::write($route, $e->getMessage());
        }
    }
    /**
     * @return string
     * 原点 2022年1月11日11:32:49 微信支付通用回调
     */
    public function weChatOrder()
    {
        try {
            $wechat = \WeChat\Pay::instance(config('pay')['wechat']);
            $data = $wechat->getNotify();
            $route = 'callback/weChatOrder.log';
            if ($data['return_code'] === 'SUCCESS' && $data['result_code'] === 'SUCCESS') {
                $info = Order::where(['sn' => $data['out_trade_no']])->find();
                //Logs::write($route, json_encode($info));
                if ($info && $info->pay_state == 0) {
                    $result = Order::operation($info, User::where(['id' => $info['uid']])->find(), 'wechat', ['types' => 1]);
                    if ($result['code'] == 1) {
                        Logs::write($route, '成功，订单号:' . $info->sn);
                        return 'success';
                    } else {
                        Logs::write($route, '失败，订单号:' . $info->sn . '->' . $result['msg']);
                    }
                } else {
                    Logs::write($route, '未查询订单');
                }
            } else {
                Logs::write($route, '非支付回调');
                exit();
            }
            return 'success';
        } catch (\Exception $e) {
            return 'fail';
        }
    }
    /**
     * 清空购物车
     */
    public function clearCart()
    {
        $Seckill = Seckill::where('end_time', '<', time())->column('id');
        if (!empty($Seckill)) {
            Cart::where(['shoptype' => 'seckill'])->whereIn('seckill_id', $Seckill)->select()->each(function ($item) {
                $item->force()->delete($item->id);
            });
        }
        Cart::where([['create_time', '<', (time() - 86400 * 3)]])->select()->each(function ($item) {
            $item->force()->delete($item->id);
        });
    }
    //定时
    public function day()
    {
        self::automatic();
    }
    //7天自动收货
    public static function automatic()
    {
        $times = time() - 86400 * 7;
        $list = Order::where(['is_del' => 0, 'status' => 3])->where('post_time', '<=', $times)->field('id,uid,status,shoptype,commission_uid1,commission_uid2,commission2,commission1,commission2')->select();
        //print('<pre>');print_r($list);die;
        if (count($list) > 0) {
            foreach ($list as $k => $info) {
                $user = User::where(['id' => $info['uid']])->find();
                $data['status'] = 4;
                $data['receipt_time'] = time();
                if ($info->shoptype == 'lntegral' || $info->shoptype == 'coupon') {
                    $money = $info->money;
                } else {
                    $money = $info->real_amount;
                }
                $user->my_total += $money;
                $user->save();
                Order::where(['id' => $info['id'], 'is_del' => 0])->update($data);
                if ($info['commission_uid1'] != 0 && $info['commission1'] != 0) {
                    Target::Account()
                        ->basics($info['commission_uid1'], 'bonus', $info['commission1'], '一级佣金结算', '+')
                        ->lock(false) //写入流水是否上锁，默认false，开启后会大幅降低性能，开启后必须配合事务使用
                        ->createNormal($info->uid);
                }
                if ($info['commission_uid2'] != 0 && $info['commission2'] != 0) {
                    Target::Account()
                        ->basics($info['commission_uid2'], 'bonus', $info['commission2'], '二级佣金结算', '+')
                        ->lock(false) //写入流水是否上锁，默认false，开启后会大幅降低性能，开启后必须配合事务使用
                        ->createNormal($info->uid);
                }
            }
        }
    }
}
